package others.swing.p011;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

/*学生成绩管理系统 版本T1.0教务处操作学生信息界面*/
public class DDo_S extends JFrame implements ActionListener {
    Label t1, t2, t3, t4, sg1, sg2, sg3;
    //JPanel rongqi;
    JTextArea jlstudent;          //用文本框显示学生信息
    JScrollPane gundong;
    JButton b1, b2, b3;
    String y = "", q = " ", sql = "";     //ad表示即将要访问的数据库名称（即访问者的身份），y表示访问者的用户名
    int i = 0, j = 0;
    String id[], n[], sc[], c1[], c2[], c3[];    //学号，姓名，班级，三门课程
    DBAccess db = new DBAccess("SMSystem", "", "");//连接数据库

    public DDo_S(String x) {
        y = x;
        t1 = new Label("欢迎您! " + y);
        t2 = new Label("所有学生信息如下：");
        t3 = new Label("学号");
        t4 = new Label("姓名");
        sg1 = new Label("课程一");
        sg2 = new Label("课程二");
        sg3 = new Label("课程三");

        jlstudent = new JTextArea(6, 10);      //
        gundong = new JScrollPane(jlstudent);//出现滚动条
        b1 = new JButton("修改学生信息");
        b2 = new JButton("查看学生信息");
        b3 = new JButton("返回");
        setLayout(null);

        //设置标签大小和所在位置
        t1.setBounds(40, 30, 180, 50);
        b1.setBounds(250, 120, 150, 50);
        b2.setBounds(420, 120, 140, 50);
        b3.setBounds(700, 500, 80, 50);

        t2.setBounds(40, 150, 200, 50);
        t3.setBounds(60, 200, 105, 50);
        t4.setBounds(165, 200, 105, 50);
        sg1.setBounds(275, 200, 105, 50);
        sg2.setBounds(385, 200, 105, 50);
        sg3.setBounds(495, 200, 105, 50);
        gundong.setBounds(50, 260, 600, 250);
        //设置标签内的字体
        t1.setFont(new Font("楷体", 1, 20));//字体大小
        t2.setFont(new Font("楷体", 1, 20));//字体大小
        t3.setFont(new Font("楷体", 1, 20));//字体大小
        t4.setFont(new Font("楷体", 1, 20));//字体大小
        jlstudent.setFont(new Font("楷体", 1, 15));//字体大小
        sg1.setFont(new Font("楷体", 1, 20));//字体大小
        sg2.setFont(new Font("楷体", 1, 20));//字体大小
        sg3.setFont(new Font("楷体", 1, 20));//字体大小

        b1.setFont(new Font("楷体", 1, 15));//字体大小
        b2.setFont(new Font("楷体", 1, 15));//字体大小
        b3.setFont(new Font("楷体", 1, 15));//字体大小

        t1.setForeground(Color.BLUE);//字体颜色
        t2.setForeground(Color.BLUE);//字体颜色
        jlstudent.setForeground(Color.BLUE);//列表框字体颜色设为蓝色
        jlstudent.setCaretPosition(jlstudent.getText().length());//让滚动条自行向下滚动，显示最新输出的内容
        b1.setForeground(Color.BLUE);//字体颜色
        b2.setForeground(Color.BLUE);//字体颜色
        b3.setForeground(Color.BLUE);//字体颜色
        add(t1);
        add(t2);
        add(t3);
        add(t4);
        add(sg1);
        add(sg2);
        add(sg3);
        add(b1);
        add(b2);
        add(b3);
        add(gundong);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);

        //jlstudent.addListSelectionListener(this);
        setSize(800, 600);
        setVisible(true);
    }

    int viewSMessage_S()                            //得到该学生的成绩信息
    {
        int a = 0;        //a=0时是未读取学生信息或者没读取成功
        try {
            db.dbcon();
            sql = "SELECT * FROM student";
            db.dbSelect(sql);
            j = 0;   //第二次点击查看学生成绩是时数组返回开头位置开始存储信息
            while (db.rs.next()) {
                j++;
            }

            id = new String[j];
            n = new String[j];
            sc = new String[j];
            c1 = new String[j];
            c2 = new String[j];
            c3 = new String[j];
            db.rs.beforeFirst();   //游标返回开头位置
            for (i = 0; i < j; i++) {
                db.rs.next();
                id[i] = db.rs.getString(2);
                n[i] = db.rs.getString(3);
                sc[i] = db.rs.getString(5);
                c1[i] = db.rs.getString(6);
                c2[i] = db.rs.getString(7);
                c3[i] = db.rs.getString(8);
                a = 1;    //读取成绩成功
            }
            db.dbclose();
        } catch (SQLException sqle) {
            System.out.println(1 + sqle.toString());
        } catch (Exception e) {
            System.out.println(2 + e.toString());
        }
        return a;
    }

    public String setT()       //将学生信息输出
    {
        q = "";
        for (i = 0; i < j; i++) {
            q = q + id[i] + "   " + n[i] + "        " + c1[i] + "       " + c2[i] + "        " + c3[i] + "\n";
        }
        return q;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            new CSCourse();
        } else if (e.getSource() == b2) {
            if (viewSMessage_S() == 1) {
                jlstudent.setText("");
                jlstudent.setText(setT());
            } else {
                JOptionPane.showMessageDialog(null, "读取数据库出错！", "友情提示", 1);
            }
        } else if (e.getSource() == b3) {
            dispose();//清除界面 setVisible(false);
        }
    }
}